--require "MainScene"

KeXueYuanScene={}
KeXueYuanSceneFunc={}
KeXueYuanSceneFunc.__index = KeXueYuanSceneFunc
setmetatable(KeXueYuanScene, KeXueYuanSceneFunc)

KeXueYuanScene.NodeID = {ScienceTabelBK=1010, UpButton=1020}
local ScienceItemNodeID = {NameLabel=2001, LevelLabel=2002, ImgBK=2003, Button=2004}
KeXueYuanScene.sciences = {}
KeXueYuanScene.upTechCooldown = 0 
--KeXueYuanScene.upgradeCooldown = 0

KeXueYuanSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, KeXueYuanSceneFunc)
  l.node = CCBReader:load("KeXueYuanScene.ccbi")
  --l.node:registerScriptTouchHandler(KeXueYuanSceneFunc.onTouchLayer)
  --l.node:setTouchEnabled(true)
  print("KeXueYuanScene.getNode", KeXueYuanScene.node)
  KeXueYuanScene.instance = l
  l:init() 
  return l 
end

KeXueYuanSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(KeXueYuanScene.getNode().node)
  print("KeXueYuanScene.getScene", sc)
  KeXueYuanScene.scene = sc
  return sc
end

KeXueYuanSceneFunc.init = function(self)
  self:initTable()
  setBuildingBaseData(BuildingID.KeXueYuan, self.node)
  setObjectExtTable(self.node:getChildByTag(1020), self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  self.upBuildingButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  self.timeLabel = TypeCast(self.node:getChildByTag(1006), "CCLabelTTF")
  --self.upingDesc = TypeCast(self.node:getChildByTag(1010), "CCLabelTTF")

  --UpgradeManager.checkUpgrade(BuildingID.KeXueYuan)
  --self:setUpTechCoolDown()
  local tmp = UITimer.getUITimer("uptechcd", self, ccp(-15,15), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(725, 40))
    self.node:addChild(tmp.view)
    self.upTechTimer = tmp
    self:setEnabledUpTechButton(false)
  end
  tmp = UITimer.getUITimer(string.format("upbuildingcd_%d", BuildingID.KeXueYuan), self, ccp(160,72), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(710, 420))
    self.node:addChild(tmp.view)
    self.upBuildingTimer = tmp 
    self.timeLabel:setVisible(false) 
    self.upBuildingButton:setVisible(false)
    self.upBuildingButton:setEnabled(false)
  end 
  KeXueYuanGuide.nextStep()
end

KeXueYuanSceneFunc.initTable = function(self)
  local sciences = UserData.config.models.Technology
  local items = CCArray:create()
  local buttons = {}
  self.unlockedItems = Array:create()
  for k,v in ipairs(sciences) do
    local item = CCBReader:load("ScienceItem.ccbi")
    print("KeXueYuanScene.initTable", item, v.name)
    local lb1 = item:getChildByTag(ScienceItemNodeID.NameLabel)
    TypeCast(lb1, "CCLabelTTF"):setString(v.name)
   
    --function addButtonFromBK(node, bkid, imgName, title, action, target)
    local b = addButtonFromBK(item, ScienceItemNodeID.ImgBK, nameFromPath(v.icon), "", "onClickTech", self) 
    setObjectExtTable(b, self)
    b:setTag(100000+k)
    --local imgBK = item:getChildByTag(ScienceItemNodeID.ImgBK)
    --local sp = CCSprite:create(nameFromPath(v.icon))
    --sp:setPosition(imgBK:getPosition())
    --imgBK:getParent():addChild(sp)
    
    local b1 = item:getChildByTag(ScienceItemNodeID.Button)
    b1:setTag(k)
    setObjectExtTable(b1, self)

    local tech = Technology:create(v.id)
    local lb2 = item:getChildByTag(ScienceItemNodeID.LevelLabel)
    TypeCast(lb2, "CCLabelTTF"):setString(string.format("%d级", tech.level))
    
    local rl = tech:getUpRequireLevel() 
    local level = UserData.userinfo.host.cities2[tostring(BuildingID.KeXueYuan)].l
    print("KeXueYuanScene.initTable", tech.config.name, rl, level)
    --if tech.config.level>level or rl>level then
    if rl>level then
      local sp = CCSprite:create("mask2.png")
      local lb = CCLabelTTF:create(string.format("需要等级%d", rl), "Helvetica", 28)
      sp:setPosition(ccp(160,95))
      lb:setPosition(ccp(160,50))
      item:addChild(sp)
      item:addChild(lb)
      --setTouchEnabledAll(item, false)
      TypeCast(b1, "CCButton"):setEnabled(false)
    else
      self.unlockedItems:append(item)
      buttons[#buttons+1] = b1
    end
    item:setContentSize(CCSizeMake(320, 190))
    items:addObject(item)
  end
  self.buttons = buttons 
  if self.table ~= nil then
    --self.node:removeChild(self.table, true)
    TypeCast(self.table, "CCTableView"):setDatas(items)
    return
  end
  --local tableBK = self.node:getChildByTag(KeXueYuanScene.NodeID.ScienceTabelBK)
  --local size = tableBK:getContentSize()
  --local tableview = CCTableView:create(0, CCSizeMake(size.width-20, size.height), items)
  local tableview = CCTableView:create(0, CCSizeMake(850, 190), items)
  --local x, y = ActionUtil.leftBottomPoint(tableBK)
  tableview:setPosition(ccp(55,125))
  self.node:addChild(tableview)
  self.table = tableview
  if KeXueYuanScene.tableOffset~=nil then
    TypeCast(self.table, "CCScrollView"):setContentOffset(KeXueYuanScene.tableOffset)
  end
end

KeXueYuanSceneFunc.setEnabledUpTechButton = function(self, enable)
  for k,v in pairs(self.buttons) do
    TypeCast(v, "CCButton"):setEnabled(enable)
  end
  --[[for i=0,self.unlockedItems:size()-1 do
    local item = self.unlockedItems:at(i)
    setTouchEnabledAll(item, enable)
  end]]
end

--[[
KeXueYuanSceneFunc.setUpTechCoolDown = function(self)
  local lb = self.node:getChildByTag(1007)
  if KeXueYuanScene.upTechCooldown==0 then
    self:setEnabledUpTechButton(true)
    TypeCast(lb, "CCLabelTTF"):setString("等待招募")
  else
    self:setEnabledUpTechButton(false)
    TypeCast(lb, "CCLabelTTF"):setString(formatSecond(KeXueYuanScene.upTechCooldown))
  end
end
]]
KeXueYuanSceneFunc.onClickTech = function(self, node, event)
  local tid = node:getTag()-100000
  local tech = Technology:create(tid)
  self.curTip = tech:createTip() 
  self.node:addChild(self.curTip)
  self.curTip:registerScriptTouchHandler(KeXueYuanSceneFunc.onTouchTipLayer, false, -1111, true)
  self.curTip:setTouchEnabled(true)
end

KeXueYuanSceneFunc.onUpTechButton = function(self, node, event)
  --self:setEnabledUpTechButton(false)
  self.upingTechID = node:getTag()
  NetworkRequest.upTech(node:getTag(), "onUpTechComplete", self) 
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(self.node, false)
  KeXueYuanScene.tableOffset = TypeCast(self.table, "CCScrollView"):getContentOffset()
end

KeXueYuanSceneFunc.onUpTechComplete = function(self, data)
  print("KeXueYuanSceneFunc.onUpTechComplete")
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("研究成功", ccc3(0,255,0))
    --self:initTable()
    local tech = Technology:create(self.upingTechID)
    local tmp = UITimer.createUITimer("uptechcd", tech:getUpgradeDuration(), self, ccp(-15,15), ccp(0, 0))
    tmp.view:setPosition(ccp(725, 40))
    self.node:addChild(tmp.view)
    self.upTechTimer = tmp
  else
    --self:setEnabledUpTechButton(true)
    showMessage(res.msg, ccc3(255,255,0))
  end
  --UITimer.removeUITimer(self.upTechTimer)
  --UITimer.removeUITimer(self.upBuildingTimer)
  --MainScene.scene:removeChild(self.node, true)
  self:closeScene()
  MainScene.scene:addChild(KeXueYuanScene:getNode().node)
end
--[[
KeXueYuanSceneFunc.onTimerEnd = function(self)
  print("KeXueYuanSceneFunc.onTimerEnd")
  self:setEnabledUpTechButton(true)
end]]
KeXueYuanSceneFunc.onUpgradeButton = function(self, node, event)
  if KeXueYuanScene.upgradeCooldown==nil or KeXueYuanScene.upgradeCooldown<=0 then
    --self.upBuildingButton:setEnabled(false)
    NetworkRequest.upgradeBuilding(BuildingID.KeXueYuan, "onUpgradeComplete", self)
    LoadScene:showLoadingScene()
    --setTouchEnabledAll(self.node, false)
  else
    --JiaSuDialog:create(BuildingID.KeXueYuan, self)
  end
end

KeXueYuanSceneFunc.onUpgradeComplete = function(self, data) 
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("升级成功", ccc3(0,255,0))
    --UpgradeManager.upgradeBuilding(BuildingID.KeXueYuan)
    local bl = UserData.userinfo.host.cities2[tostring(BuildingID.KeXueYuan)].l
    local binfo = UserData.config.models[BuildingLevel[BuildingID.KeXueYuan]][bl]
    UserData.userinfo.host.cities2[tostring(BuildingID.KeXueYuan)].l = bl+1
    setBuildingBaseData(BuildingID.KeXueYuan, self.node)
    self:initTable()

    self.upBuildingTimer = UITimer.createUITimer(string.format("upbuildingcd_%d", BuildingID.KeXueYuan), binfo.coolingTime, self, ccp(160,72), ccp(0, 0))
    self.upBuildingTimer.view:setPosition(ccp(710, 420))
    self.node:addChild(self.upBuildingTimer.view)
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
  else
    showMessage(res.msg, ccc3(255,255,0))
    self.upBuildingButton:setEnabled(true)
  end
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(KeXueYuanScene:getNode().node)
  --setTouchEnabledAll(self.node, true)
end


---------------------------------------------------------------------------------------------------------------
--处理加速
KeXueYuanSceneFunc.onTimerEnd = function(self, timer)
  if timer~=nil then
    timer.view:getParent():removeChild(timer.view, true) 
  end
  if timer==self.upTechTimer then
    self:setEnabledUpTechButton(true)
    --self.upingDesc:setString("")
  else
    self.timeLabel:setVisible(true)
    self.upBuildingButton:setVisible(true)
    self.upBuildingButton:setEnabled(true)
  end
end
KeXueYuanSceneFunc.onJiaSuButton = function(self, timer, pars)
  local tmp = JiaSuSampleDialog:create(pars.leftTime, self)
  MainScene.scene:addChild(tmp.view)
  UITimer.removeUITimer(self.upTechTimer)
  UITimer.removeUITimer(self.upBuildingTimer)

  MainScene.scene:removeChild(self.node, true)

  if timer==self.upTechTimer then
    self.jiasuUpTechDlg = tmp 
  elseif timer==self.upBuildingTimer then
    self.jiasuUpBuildingDlg = tmp 
  end
end

KeXueYuanSceneFunc.onJiaSuConfirm = function(self, dlg, confirm, timereel, timeLeft)
  if confirm~=true then
    MainScene.scene:removeChild(dlg.view, true)
    MainScene.scene:addChild(KeXueYuanScene.getNode().node)
  elseif dlg==self.jiasuUpTechDlg then
    --NetworkRequest.test("onJiaSuComplete", self)
    NetworkRequest.resetTime(timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  elseif dlg==self.jiasuUpBuildingDlg then
    NetworkRequest.accelerate("Upgrading", BuildingID.KeXueYuan, timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  end
end

KeXueYuanSceneFunc.onJiaSuComplete = function(self, data)
  print("KeXueYuanSceneFunc.onJiaSuComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    if self.jiasuUpTechDlg~=nil then
      self.upTechTimer:endTimer() 
    end
    if self.jiasuUpBuildingDlg~=nil then
      self.upBuildingTimer:endTimer() 
    end
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  
  if self.jiasuUpTechDlg~=nil then
    MainScene.scene:removeChild(self.jiasuUpTechDlg.view, true)
  end
  if self.jiasuUpBuildingDlg~=nil then
    MainScene.scene:removeChild(self.jiasuUpBuildingDlg.view, true)
  end
  MainScene.scene:addChild(KeXueYuanScene.getNode().node)
end
---------------------------------------------------------------------------------------------------------------
KeXueYuanSceneFunc.onTouchTipLayer = function(event, x, y)
  if event==CCTOUCHBEGAN then
    if KeXueYuanScene.instance.curTip~=nil then
      print("onTouchTipLayer clear tip")
      KeXueYuanScene.instance.node:removeChild(KeXueYuanScene.instance.curTip, true)
      KeXueYuanScene.instance.curTip = nil
    end
  end
  return true 
end

KeXueYuanSceneFunc.closeScene = function(self)
  UITimer.removeUITimer(self.upTechTimer)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  KeXueYuanScene.instance = nil
end

KeXueYuanSceneFunc.onClose = function(self, node, event)
  print("KeXueYuanSceneFunc.onClose")
  KeXueYuanScene.tableOffset = nil
  self:closeScene()
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end
